package com.hammer.m3u8downloader.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hammer.m3u8downloader.R;


public class InputDialog extends AlertDialog {
    private View root;
    private Context context;
    private TextView tv_title;
    private EditText et_input;
    private View v_line;

    private OnSubmitListener onSubmitListener;

    private boolean autoDismiss = true;

    private View.OnClickListener positiveListener = null;
    private View.OnClickListener negativeListener = null;
    private View.OnClickListener neutralListener = null;

    public InputDialog(Context context) {
        super(context, R.style.MyAlertDialogStyle);
        this.context = context;
        initViews();
    }

    private void initViews() {
        this.root = View.inflate(context, R.layout.dialog_input, null);
        this.tv_title = root.findViewById(R.id.tv_title);
        this.et_input = root.findViewById(R.id.et_input);
        this.v_line = root.findViewById(R.id.v_line);
        this.et_input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                Resources resources = context.getResources();
                v_line.setBackgroundColor(b ? resources.getColor(R.color.colorAccent) : resources.getColor(R.color.divider_color));
            }
        });
        this.setView(root);
    }

    public InputDialog setDialogTitle(String title) {
        this.tv_title.setText(title);
        return this;
    }

    public InputDialog setDialogHint(String hint) {
        this.et_input.setHint(hint);
        return this;
    }

    public InputDialog setCancleable(boolean cancleable) {
        this.setCancelable(cancleable);
        return this;
    }

    public InputDialog setAutoDismiss(boolean autoDismiss) {
        this.autoDismiss = autoDismiss;
        return this;
    }

    public InputDialog setPositiveButton(String text, View.OnClickListener onClickListener) {
        OnClickListener nullClick = null;
        this.positiveListener = onClickListener;
        this.setButton(android.app.AlertDialog.BUTTON_POSITIVE, text, nullClick);
        return this;
    }

    public InputDialog setNegativeButton(String text, View.OnClickListener onClickListener) {
        OnClickListener nullClick = null;
        this.negativeListener = onClickListener;
        this.setButton(android.app.AlertDialog.BUTTON_NEGATIVE, text, nullClick);
        return this;
    }

    public InputDialog setNeutralButton(String text, View.OnClickListener onClickListener) {
        OnClickListener nullClick = null;
        this.neutralListener = onClickListener;
        this.setButton(android.app.AlertDialog.BUTTON_NEUTRAL, text, nullClick);
        return this;
    }


    public InputDialog setOnSubmitListener(OnSubmitListener onSubmitListener) {
        this.onSubmitListener = onSubmitListener;
        return this;
    }


    /**
     * 提交输入的数据并传回是否隐藏dialog
     */
    public interface OnSubmitListener {
        boolean onSubmit(@NonNull String text);
    }

    private void setButtonListener() {
        this.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                if (positiveListener != null) {
                    Button button = getButton(DialogInterface.BUTTON_POSITIVE);
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            positiveListener.onClick(view);
                            if (onSubmitListener != null) {
                                boolean dismiss = onSubmitListener.onSubmit(et_input.getText().toString());
                                if (dismiss) {
                                    dismiss();
                                }
                            } else {
                                if (autoDismiss) {
                                    dismiss();
                                }
                            }
                        }
                    });
                }

                if (negativeListener != null) {
                    Button button1 = getButton(DialogInterface.BUTTON_NEGATIVE);
                    button1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            negativeListener.onClick(view);
                            if (autoDismiss) {
                                dismiss();
                            }
                        }
                    });
                }

                if (neutralListener != null) {
                    Button button2 = getButton(DialogInterface.BUTTON_NEUTRAL);
                    button2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            neutralListener.onClick(view);
                            if (autoDismiss) {
                                dismiss();
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    public void show() {
        setButtonListener();
        super.show();
    }
}
